$(function() {
	var FangTanGrid = $("#FangTanGrid");ftGridAction
	var ftGridAction = $("#ftGridAction");
	FangTanGrid.datagrid({
		url : 'fangtan/list',
		fit : true,
		border : false,
		rownumbers : true,
		pagination : true,
		fitColumns : true,// 自适应大小
		columns : [ [
				{
					field : 'name1',
					title : '学生姓名',
					align : 'center',
					width : 100
				},
				{
					field : 'class_name',
					title : '学生班级',
					align : 'center',
					width : 100
				},
				{
					field : 'fangtan_fangshi',
					title : '访谈方式',
					align : 'center',
					width : 100
				},
				{
					field : 'name2',
					title : '老师',
					align : 'center',
					width : 100
				},
				{
					field : 'fangtan_content',
					title : '访谈内容',
					align : 'center',
					width : 100,
					formatter : function(value) {
						if (value==null||value=='') {
							return "<span  style='color:red;'  title='暂无'>暂无</span>";
						}else{
							return "<span title='" + value + "'>" + value
							+ "</span>";
						}
					}
				},
				{
					field : 'fangtan_time',
					title : '访谈时间',
					sortable : true,
					align : 'center',
					width : 120,
					formatter : function(value, row, index) {
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 100,
					formatter : function(value, row, index) {
						/*var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr delete">删除</a>');*/
						return ftGridAction.children("a.ctr").attr('data-id',
								row.id).attr('data-index', index)
								.end().html();
					}
				} ] ]
	});
	var studentinfoName = $("#studentinfoName");
	var classTeacher = $("#classTeacher");
	var classId = $("#classId");
	$("#fangTanSearch").on('click', function() {
		FangTanGrid.datagrid("load", {
			interviewMethod : "%" + studentinfoName.val() + "%",
			interviewContent : "%" + classTeacher.val() + "%",
			interviewId : classId.val()
		});
	});
	var fangTanSearchForm = $("#fangTanSearchForm");
	$("#fangTanReset").on('click', function() {
		fangTanSearchForm.form("clear");
		FangTanGrid.datagrid("load", {})
	});
	var gridPanel = FangTanGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = FangTanGrid.datagrid("getRows");
		var id = rows[index].fangtan_id;
		fangtan(id);
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var rows = FangTanGrid.datagrid("getRows");
		var id = rows[index].fangtan_id;
		$.messager.confirm("提示", "你确定要删除此数据？", function(r) {
			if (r) {
				$.get("fangtan/delete/" + id, function(rs) {
					if (rs.success) {
						FangTanGrid.datagrid("reload");
					}else{
						$.message.alert("提示",rs.message)
					}
				});
			}
		});
	});

	function fangtan(id) {
		var dialog = $("<div/>").dialog(
				{
					href : 'jiaoxue/jx-add-fangtan' + (id ? ("/" + id) : ""),
					title : '访谈',
					height : '450',
					width : '507',
					modal : true,
					onClose : function() {
						$(this).dialog("destroy");
					},
					buttons : [
							{
								iconCls : 'icon-save',
								text : '添加',
								handler : function() {
									var addFangtanForm = $("#addFangtanForm");
									if (addFangtanForm.form('validate')) {
										$.post("fangtan/save", addFangtanForm
												.serialize(), function(rs) {
											if (rs.success) {
												FangTanGrid.datagrid("reload");
												dialog.dialog("close");
											} else {
												$.messager.alert("提示",
														rs.message);
											}
										});
									}
								}
							}, {
								iconCls : 'icon-clear',
								text : '取消',
								handler : function() {
									dialog.dialog("close");
								}
							} ]
				});
	}

});